import os

# 两个文件夹的路径
folder1_path = 'samples/train/images'
folder2_path = 'samples/train/labels'

# 获取两个文件夹中的文件列表
folder1_files = os.listdir(folder1_path)
folder2_files = os.listdir(folder2_path)

# 提取文件名称（去除后缀）
folder1_file_names = [os.path.splitext(file)[0] for file in folder1_files]
folder2_file_names = [os.path.splitext(file)[0] for file in folder2_files]

# 找出没有对应的文件
files_to_delete_in_folder1 = [file for file in folder1_file_names if file not in folder2_file_names]
files_to_delete_in_folder2 = [file for file in folder2_file_names if file not in folder1_file_names]

# 删除没有对应的文件
for file_name in files_to_delete_in_folder1:
    file_path = os.path.join(folder1_path, file_name + '.jpg')  # 将 'extension' 替换为文件后缀
    os.remove(file_path)

for file_name in files_to_delete_in_folder2:
    file_path = os.path.join(folder2_path, file_name + '.xml')  # 将 'extension' 替换为文件后缀
    os.remove(file_path)

